package tianqi.controller.email;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tianqi.entity.Email;
import tianqi.entity.UserY;
import tianqi.service.Email.impl.EmailServiceImpl;
import tianqi.utils.mail.MailSenderInfo;
import tianqi.utils.mail.SimpleMailSender;

@Controller
public class EmailController {

	@Autowired
	private EmailServiceImpl emailServiceImpl;

	/**
	 * 跳转到Email界面
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/toEmail")
	public String toEmail(HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		// 根据用户ID 查询此用户是否注册过邮箱
		Email queryEmailByUId = emailServiceImpl.queryEmailByUId(userY);
		if (queryEmailByUId == null) {
			m.addAttribute("mail", "0");
			return "senMail";
		}
		
		MailSenderInfo mi = new MailSenderInfo();
		mi.setUserName(queryEmailByUId.getEmail());
		mi.setPassword(queryEmailByUId.getEpwd());
		req.getSession().setAttribute("mi", mi);
		
		
		m.addAttribute("mail", "1");
		m.addAttribute("email", queryEmailByUId);
		return "senMail";
	}
	
	
	/**
	 * 如果 调用Email接口  用户需要 注册一个邮箱，从而得知用户的用户名及密码
	 * @param email
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/registEmail")
	public String registEmail(Email email, HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		email.setUserid(userY.getUid());
		try {
			emailServiceImpl.saveEmail(email);
		} catch (Exception e) {
			m.addAttribute("mail", "0");
			return "senMail";
		}

		MailSenderInfo mi = new MailSenderInfo();
		mi.setUserName(email.getEmail());
		mi.setPassword(email.getEpwd());
		req.getSession().setAttribute("mi", mi);

		m.addAttribute("mail", "1");
		m.addAttribute("email", email);
		return "senMail";
	}
	
	/**
	 * 发送邮件
	 * @param toaddress
	 * @param subJect
	 * @param content
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/SendEmail")
	@ResponseBody
	public String SendEmail(String toaddress, String subJect, String content, HttpServletRequest req,
			HttpServletResponse res, Model m) {
		MailSenderInfo mailInfo = (MailSenderInfo) req.getSession().getAttribute("mi");
		if (mailInfo == null) {
			return "sessionOut";
		}

		if (mailInfo.getUserName().indexOf("qq") > 0) {
			mailInfo.setMailServerHost("smtp.qq.com");
			mailInfo.setMailServerPort("465");
			mailInfo.setQQ(true);
			mailInfo.setValidate(true);
			mailInfo.setUserName(mailInfo.getUserName());
			mailInfo.setPassword(mailInfo.getPassword());// 您的邮箱密码qq
			mailInfo.setFromAddress(mailInfo.getUserName());
		} else if (mailInfo.getUserName().indexOf("163") > 0) {
			mailInfo.setMailServerHost("smtp.163.com");
			mailInfo.setMailServerPort("25");
			mailInfo.setQQ(false);
			mailInfo.setValidate(true);
			mailInfo.setUserName(mailInfo.getUserName());
			mailInfo.setPassword(mailInfo.getPassword());// 您的邮箱密码qq
			mailInfo.setFromAddress(mailInfo.getUserName());
		} else {
			return "nocan";
		}
		SimpleMailSender sms = new SimpleMailSender();
		if (toaddress.indexOf(";") > 0) {
			String[] split = toaddress.split(";");
			
			for (String string : split) {
				mailInfo.setToAddress(string);
				mailInfo.setSubject(subJect);
				mailInfo.setContent(content);
				// mailInfo.setFilePath("G:/ads/建表说明.xlsx");
//				mailInfo.setImgPath("G:/ads/1.jpg");
				try {
					sms.sendFileMail(mailInfo);
				} catch (Exception e) {
					return "error";
				}
				
			}
			
			
		}else {
			mailInfo.setToAddress(toaddress);
			mailInfo.setSubject(subJect);
			mailInfo.setContent(content);
			// mailInfo.setFilePath("G:/ads/建表说明.xlsx");
//			mailInfo.setImgPath("G:/ads/1.jpg");
			try {
				sms.sendFileMail(mailInfo);
			} catch (Exception e) {
				return "error";
			}
		}
		return "ok";
	}
	

}
